﻿using System;
using System.Collections.Specialized;
using eProject.Model;

namespace eProject.ViewModel
{
    public class EmployeeViewModel : ViewModelBase
    {

        internal EmployeeViewModel(CommonManager manager, Employee employee)
        {
            if (employee == null) throw new ArgumentNullException("employee");
            if (manager == null) throw new ArgumentNullException("manager");

            Employee = employee;            
            _manager = manager;

            INotifyCollectionChanged iempls = _manager.Employees;
            iempls.CollectionChanged += OnCollectionChanged;
        }

        internal Employee Employee { get; private set; }

        public string Name 
        { 
            get { return Employee.Name; } 
            set
            {
                if (value == Name) return;
                Employee.Name = value;
                InvokePropertyChanged("Name");
            }
        }

        public string EMail
        {
            get { return Employee.Email; }
            set
            {
                if (value == EMail) return;
                Employee.Email = value;
                InvokePropertyChanged("EMail");
            }
        }

        public string Surname
        {
            get { return Employee.Surname; }
            set
            {
                if (value == Surname) return;
                Employee.Surname = value;
                InvokePropertyChanged("Surname");
            }
        }

        public string PhoneNumber
        {
            get { return Employee.PhoneNumber; }
            set
            {
                if (value == PhoneNumber) return;
                Employee.PhoneNumber = value;
                InvokePropertyChanged("PhoneNumber");
            }
        }

        public int Index
        {
            get { return _manager.Employees.IndexOf(this); }
        }

        public double GetManHours(DateTime date)
        {
            return _manager.GetManHours(this, date);
        }

        public void Unlink()
        {
            INotifyCollectionChanged iempls = _manager.Employees;
            iempls.CollectionChanged -= OnCollectionChanged;
        }

        private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
        {
            InvokePropertyChanged("Index");
        }

        private readonly CommonManager _manager;
     
    }
}
